//
//  FTYUser.m
//  RedWine
//
//  Created by Liufangfang on 2017/1/9.
//  Copyright © 2017年 HuiBangKe. All rights reserved.
//

#import "FTYUser.h"

@implementation FTYUser

#pragma mark - User Manager

static FTYUser * manager = nil;

+ (instancetype)sharedUser{

    //3.创建当前类的对象
    if (manager == nil) {
        manager = [[FTYUser alloc]init];
    }
    return manager;
}

- (NSUserDefaults *)defaults{
    
    return [NSUserDefaults standardUserDefaults];
}

#pragma mark - UserInfo Write

/**
 当前用户是否登录状态
 */
- (void)writeUserIsLogin:(BOOL)isLogin {
    
    [[self defaults] setBool:isLogin forKey:USERSUCCESSFUL];
    [[self defaults] synchronize];
}

/**
 当前登录的用户身份Id(后台id)
 */
- (void)writeUserID:(NSString *)userID {
    
    [[self defaults] setObject:userID forKey:USERID];
    [self synchronize];
}

/**
 当前登录的用户账号
 */
- (void)writeUserToken:(NSString *)userToken{

    [[self defaults] setObject:userToken forKey:USERTOKEN];
    [self synchronize];
}

- (void)writeUserPhoneNumber:(NSString *)userPhoneNumber{

    [[self defaults] setObject:userPhoneNumber forKey:USERPHONENUM];
    [self synchronize];
}

- (void)writeUserName:(NSString *)userName{

    [[self defaults] setObject:userName forKey:USERNAME];
    [self synchronize];
}

- (void)writeUserSex:(NSString *)userSex{

    [[self defaults] setObject:userSex forKey:USERSEX];
    [self synchronize];
}

- (void)writeUserAvatar:(NSString *)userAvatar{

    [[self defaults] setObject:userAvatar forKey:USERHEADIMGURL];
    [self synchronize];
}

- (void)writeUserAccountBalance:(NSString *)userAccountBalance{

    [[self defaults] setObject:userAccountBalance forKey:UserAccountBalance];
    [self synchronize];
}

- (void)writeUserWineBalance:(NSString *)userWineBalance{

    [[self defaults] setObject:userWineBalance forKey:UserWineBalance];
    [self synchronize];
}

- (void)writeUserBenefit:(NSString *)userBenefit{

    [[self defaults] setObject:userBenefit forKey:UserBenefit];
}

- (void)writeUserShopCart:(ShopCartModel *)shopCart
{
    NSDictionary *shopCartDict = [[self defaults] objectForKey:UserShopCart];
    ShopCartModel *shopCartItem;
    NSString *goodsId = [NSString stringWithFormat:@"%ld",shopCart.goods_id];
    
    if ([shopCartDict isKindOfClass:[NSDictionary class]]) {
        NSData *data = [shopCartDict objectForKey:goodsId];
        if (data) {
            shopCartItem = [NSKeyedUnarchiver unarchiveObjectWithData:data];
            shopCartItem.goods_num = shopCartItem.goods_num+shopCart.goods_num;
        }else{
            shopCartItem = shopCart;
        }
        
        
    }else{
        shopCartDict = [NSDictionary dictionary];
    }
    
    NSMutableDictionary *updateDict = [NSMutableDictionary dictionaryWithObjects:[shopCartDict allValues] forKeys:[shopCartDict allKeys]];
    NSData *updateData = [NSKeyedArchiver archivedDataWithRootObject:shopCartItem];
    [updateDict setObject:updateData forKey:goodsId];
    
    [[self defaults] setObject:updateDict forKey:UserShopCart];
    
}
- (void)synchronize{
    [[self defaults] synchronize];
}




#pragma mark - UserInfo Read

/**
 当前用户是否登录状态
 */
- (BOOL)isUserLogin {
    
    return [[self defaults] boolForKey:USERSUCCESSFUL];
}

/**
 当前登录的用户身份Id(后台id)
 */
- (NSString *)userID {
    NSString * userId = [[self defaults] objectForKey:USERID];
    if (userId == nil) {
        return @"";
    }
    return userId;
}

-(NSString *)userToken {
    return [[self defaults] objectForKey:USERTOKEN];
}

- (NSString *)userPhoneNumber{
    return [[self defaults] objectForKey:USERPHONENUM];
}

- (NSString *)userName{
    return [[self defaults] objectForKey:USERNAME];
}

- (NSString *)userSex{
    return [[self defaults] objectForKey:USERSEX];
}

- (NSString *)userAvatar{
    return [[self defaults] objectForKey:USERHEADIMGURL];
}

- (NSString *)userAccountBalance{
    return [[self defaults] objectForKey:UserAccountBalance];
}

- (NSString *)userWineBalance{
    return [[self defaults] objectForKey:UserWineBalance];
}

- (NSString *)userBenefit{
    return [[self defaults] objectForKey:UserBenefit];
}



- (ShopCartModel *)userShopCartByGoodsId:(NSString *)goodsId
{
    NSDictionary *shopCartDict = [[self defaults] objectForKey:UserShopCart];
    if ([shopCartDict isKindOfClass:[NSDictionary class]]) {
        NSData *data = [shopCartDict objectForKey:goodsId];
        
        if (data) {
            ShopCartModel *shopCartItem = [NSKeyedUnarchiver unarchiveObjectWithData:data];
            return shopCartItem;
        }
    }
    
    
    return nil;
}

#pragma mark - User LoginOut

/**
 退出登录
 */
- (void)userLogOut {
    
    [[self defaults] setBool:NO forKey:USERSUCCESSFUL];
    [[self defaults] setObject:@"" forKey:USERID];
    [[self defaults] setObject:@"" forKey:USERTOKEN];
    [[self defaults] setObject:@"" forKey:USERPHONENUM];
    [[self defaults] setObject:@"" forKey:USERNAME];
    [[self defaults] setObject:@"" forKey:USERSEX];
    [[self defaults] setObject:@"" forKey:USERHEADIMGURL];
    [[self defaults] setObject:@"" forKey:UserAccountBalance];
    [[self defaults] setObject:@"" forKey:UserWineBalance];
    [[self defaults] setObject:@"" forKey:UserBenefit];
    [[self defaults] setObject:@"" forKey:UserShopCart];
    [[self defaults] synchronize];
}

@end
